﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Assertions;
using UnityEngine.UI;

namespace Nirvana
{
    /// <summary>
    /// 用于toggle切换图形对象的颜色，可带过度动画
    /// </summary>
    [RequireComponent(typeof(Toggle))]
    [AddComponentMenu("Nirvana/UI/Control/Toggle Color")]
    public sealed class ToggleColor : MonoBehaviour
    {

        [SerializeField]
        [Tooltip("The graphic target to change color.")]
        private Graphic target;

        [SerializeField]
        [Tooltip("The toggled color.")]
        private Color toggledColor = Color.white;

        [SerializeField]
        [Tooltip("The transit time.")]
        private float transitTime = 0.1f;

        [SerializeField]
        [Tooltip("Whether auto hide the target when the alpha is zero.")]
        private bool autoHide = true;

        private Toggle _toggle;
        private Color defaultColor,startColor,endColor;
        private float curTime = -1f;

        private void Awake()
        {
            this._toggle = base.GetComponent<Toggle>();
            Assert.IsNotNull<Toggle>(this._toggle);
            if (this.target != null)
            {
                this.defaultColor = this.target.color;
            }
            this.OnValueChanged(this._toggle.isOn);
            this._toggle.onValueChanged.AddListener(OnValueChanged);
        }
        private void OnEnable()
        {
            this.OnValueChanged(this._toggle.isOn);
        }

        private void Update()
        {
            if (this.target == null)
            {
                return;
            }
            if (this.curTime > 0f)
            {
                this.curTime -= Time.deltaTime;
                if (this.curTime <= 0f)
                {
                    this.target.color = this.endColor;
                    if (this.autoHide && Mathf.Approximately(this.target.color.a, 0f))
                    {
                        this.target.enabled = false;
                    }
                }
                else
                {
                    this.target.color = Color.Lerp(this.startColor, this.endColor, 1f - this.curTime / this.transitTime);
                }
            }
        }

        private void OnDestroy()
        {
            if (this._toggle != null)
            {
                this._toggle.onValueChanged.RemoveListener(OnValueChanged);
            }
        }

        private void OnValueChanged(bool IsOn)
        {
            if (this.target == null)
            {
                return;
            }
            if (this.transitTime > 0f)
            {
                this.startColor = this.target.color;
                this.endColor = ((!IsOn) ? this.defaultColor : this.toggledColor);
                this.curTime = this.transitTime;
                if (this.autoHide)
                {
                    this.target.enabled = true;
                }
            }
            else if (IsOn)
            {
                this.target.color = this.toggledColor;
                if (this.autoHide)
                {
                    this.target.enabled = true;
                }
            }
            else
            {
                this.target.color = this.defaultColor;
                if (this.autoHide && Mathf.Approximately(this.target.color.a, 0f))
                {
                    this.target.enabled = false;
                }
            }
        }


    }
}

